package service

import (
	"blog_go/internal/model"
	"blog_go/internal/repository"
	"errors"
)

type ArticleCommentService interface {
	QueryComment(articleId int, userId int) ([]*model.ArticleCommentTreeList, error)
	QueryAllComment(userId int, userType int) ([]*model.ArticleCommentManage, error)
	AddComment(comment model.ArticleComment, userId int) error
	DelComment(commentId int, userId int, userType int) error
	ReviewComment(commentIds []int, status int) error
	ReviewAllComment() error
	LikeArticleComment(commentId int, userId int, isLike bool) error
	DisLikeArticleComment(commentId int, userId int, isDisLike bool) error
}

type articleCommentService struct {
	*Service
	articleCommentRepository repository.ArticleCommentRepository
}

func NewArticleCommentService(service *Service, articleCommentRepository repository.ArticleCommentRepository) ArticleCommentService {
	return &articleCommentService{
		Service:                  service,
		articleCommentRepository: articleCommentRepository,
	}
}

func (s *articleCommentService) QueryComment(articleId int, userId int) ([]*model.ArticleCommentTreeList, error) {
	return s.articleCommentRepository.QueryComment(articleId, userId)
}

func (s *articleCommentService) QueryAllComment(userId int, userType int) ([]*model.ArticleCommentManage, error) {
	return s.articleCommentRepository.QueryAllComment(userId, userType)
}
func (s *articleCommentService) AddComment(comment model.ArticleComment, userId int) error {
	if comment.UserId != userId {
		return errors.New("无法替他人添加评论")
	}

	if comment.ArticleId == 0 || comment.Content == "" {
		return errors.New("参数为空")
	}

	return s.articleCommentRepository.AddComment(comment)
}
func (s *articleCommentService) DelComment(commentId int, userId int, userType int) error {
	return s.articleCommentRepository.DelComment(commentId, userId, userType)
}
func (s *articleCommentService) ReviewComment(commentIds []int, status int) error {
	return s.articleCommentRepository.ReviewComment(commentIds, status)
}

func (s *articleCommentService) LikeArticleComment(commentId int, userId int, isLike bool) error {
	if isLike {
		return s.articleCommentRepository.LikeArticleComment(commentId, userId)
	} else {
		return s.articleCommentRepository.UnLikeArticleComment(commentId, userId)
	}
}

func (s *articleCommentService) DisLikeArticleComment(commentId int, userId int, isDisLike bool) error {
	if isDisLike {
		return s.articleCommentRepository.DisLikeArticleComment(commentId, userId)
	} else {
		return s.articleCommentRepository.UnDisLikeArticleComment(commentId, userId)
	}
}

func (s *articleCommentService) ReviewAllComment() error {
	return s.articleCommentRepository.ReviewAllComment()
}
